🏷️ computer-science 🏷️ personal-opinion 🏷️ tech

# Configuration, Environments and Secrets

# Configuration

A way to easily find and change the behavior of a program based on different intentions by changing the values of appropriate variables.

For example:

So, configuration should be:

# Environments

A collection of values for different configuration variables defining a particular intention.

For example:

So, environments should be:

# Secrets

Values that are not meant to be shared with the world.

For example:

So, secrets should be:

# Combining All 3

# Proof of Concept

# Python implementation

# settings.py

import os
from pathlib import Path
from typing import Callable, TypeVar

T = TypeVar("T")

def env(**kwargs: T | Callable[[], T]) -> T:
    val = kwargs[ENVIRONMENT]
    return val() if callable(val) else val

def readfile(path: str | Path, type_: Callable[[str], T] = str) -> Callable[[], T]:
    return lambda: type_(Path(path).read_text().strip())


ENVIRONMENT = os.environ.get("ENVIRONMENT", "dev")

DATABASE_URL = env(
    dev="dev_url",
    prod=readfile("/home/user/secrets/database_url"),
)
copy success

# My new TOML based generic implementation in Rust

Tool: http://settingspec.arijitbasu.in/

[spec]                               # Optional: Declare the specification here
profile.key = "SETTINGSPEC_PROFILE"  # Default: Environment variable name to switch between profiles
profile.options = [                  # Optional: Enables strict checking of per-profile declarations
  "dev",
  "stage",
  "prod",
]
profile.default = "dev"  # Optional: Default profile when the switch is not set

[settings]                   # Declare the settings here with syntax: `<key>._.<profile>.<directive> = <value>`
key1._.default.val = "val1"  # Default value for all profiles
key1._.prod.val = "prod1"    # Override the default value when SETTINGSPEC_PROFILE=prod

key2._ = {  # Strictly define different values for different profiles
  dev.val = "dev2",
  stage.val = "stage2",
  prod.val = "prod2",
}
copy success


✏️ Edit this note